//
//  PywUserCenterCell.m
//  PywSdk
//
//  Created by smallwave on 16/4/9.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUserCenterCell.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"
#import "PywCommonManager.h"
#import "PywPlatformMain.h"
#import "PywAppInfo.h"

#define ARROW_IMG_WIDTH [UIImage imageWithName:@"icon_next_normal"].size.width

@interface PywUserCenterCell()

@property (nonatomic, strong) UILabel       *leftLabel;
@property (nonatomic, strong) UILabel       *rightLabel;
@property (nonatomic, strong) UIImageView   *arrowImg;
@property (nonatomic, strong) UIButton      *selectBtn;
@property (nonatomic, strong) UIView        *line;

@property (nonatomic, copy) NSString *buttonTitle;

@end

@implementation PywUserCenterCell

#pragma mark - override
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        [self setUpLayoutSubviews];
    }
    return self;
}



#pragma mark - setUpSubviews
- (void)setUpSubviews{
    
    [self.contentView addSubview:self.leftLabel];
    [self.contentView addSubview:self.rightLabel];
    [self.contentView addSubview:self.arrowImg];
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.line];
    
    [self.selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - event method
- (void)selectBtnAction:(UIButton *)btn{
    [self endEditing:YES];
    if ([self.cellBtnDelegate respondsToSelector:@selector(userCenterCell:SelectedButton:)]) {
        [self.cellBtnDelegate userCenterCell:self SelectedButton:btn];
    }
}

#pragma mark -
- (void)setListModel:(PywUserListModel *)listModel
{
    _listModel = listModel;
    self.selectBtn.hidden = YES;
    self.leftTitle = listModel.title;
    self.rightTitle = listModel.value;
    [self setAccessibilityIdentifier:listModel.accessibilityId];
    if (listModel.listType == ListType_account) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setAccountInfoTitle];
    }else {
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        switch (listModel.listType) {
            case ListType_loginPwd:
            {
                self.rightLabel.textColor = RGB(255, 170, 0, 1);
            }
                break;
            case ListType_payPwd:
            {
                if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Account) {
                    self.arrowImg.image = [UIImage imageWithName:@""];
                }
            }
                break;
                
            default:
                break;
        }
    }

}
/**
 *  Update 账号信息
 */
- (void)setAccountInfoTitle
{
    self.selectBtn.hidden = NO;
    self.rightTitle = @"";
    if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Passport) {
        self.buttonTitle = @"账号管理";
        // 设置手机号码的中间4为显示为@“*”号
        NSMutableString *passport = [[NSMutableString alloc] initWithString:self.listModel.value];
        if (passport.length > 8) {
            [passport replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        }
        self.leftTitle = [NSString stringWithFormat:@"欢迎您，%@",passport];
    } else {
        self.leftTitle = [NSString stringWithFormat:@"欢迎您，%@",self.listModel.value];
        if ([PywPlatformMain sharedPlatformMain].passport.length != 0) {
            self.selectBtn.hidden = YES;
            self.rightTitle = @"已绑定手机";
        } else {
            self.selectBtn.hidden = NO;
            self.buttonTitle = @"绑定手机";
        }
    }
}

#pragma mark - setter and getter
- (void)setLeftTitle:(NSString *)leftTitle{
    _leftTitle = leftTitle;
    
    if (self.listModel.listType == ListType_account) {
        // 更改约束
        __weak typeof(self) weakSelf = self;
        [self.leftLabel mas_remakeConstraints:^(PywMASConstraintMaker *make) {
            
            make.centerY.equalTo(weakSelf);
            make.left.equalTo(weakSelf.mas_left).offset(20);
            make.right.equalTo(weakSelf.selectBtn.mas_left);
        }];
        
        // 判断账号是否已认证，并显示
        if ([PywPlatformMain sharedPlatformMain].is_verify) { // 已认证
            NSString *realNameAuthLeftTitleStr = [NSString stringWithFormat:@"%@%@",self.leftTitle,@" 已认证"];
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:realNameAuthLeftTitleStr];
            // 通行证账号 显示
            [attrStr addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:15]
                            range:NSMakeRange(4, attrStr.length - 4)];
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:RGB(255, 170, 0, 1)
                            range:NSMakeRange(4, attrStr.length - 4)];
            
            // 已认证 显示
            [attrStr addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:12]
                            range:NSMakeRange(4 + self.listModel.value.length, attrStr.length - (4 + self.listModel.value.length))];
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:UIColorFromRGB(0xF57300)
                            range:NSMakeRange(4 + self.listModel.value.length, attrStr.length - (4 + self.listModel.value.length))];
            
            self.leftLabel.attributedText = attrStr;
        }else{  // 未认证
            
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.leftTitle];
            [attrStr addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:15]
                            range:NSMakeRange(4, attrStr.length - 4)];
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:RGB(255, 170, 0, 1)
                            range:NSMakeRange(4, attrStr.length - 4)];
            self.leftLabel.attributedText = attrStr;
        }
        
        self.rightLabel.textColor = RGB(153, 153, 153, 1);
    }else{
        
        self.leftLabel.text = leftTitle;
        self.arrowImg.image = [UIImage imageWithName:@"icon_next_normal"];
    }
}
- (void)setRightTitle:(NSString *)rightTitle{
    _rightTitle = rightTitle;
    
    self.rightLabel.text = rightTitle;
}
- (void)setButtonTitle:(NSString *)buttonTitle{
    _buttonTitle = buttonTitle;
    
    [_selectBtn setTitle:_buttonTitle forState:UIControlStateNormal];
}
- (void)setSelectBtnHidden:(BOOL)selectBtnHidden{
    self.selectBtn.hidden = selectBtnHidden;
}

#pragma mark - layoutSubviews
-(void)setUpLayoutSubviews{
    
    __weak typeof(self) weakSelf = self;
    [self.leftLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(20);
        make.width.equalTo(weakSelf.mas_width).multipliedBy(0.5);
    }];
    
    [self.rightLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.leftLabel.mas_right);
        make.right.equalTo(weakSelf.mas_right).offset(- ARROW_IMG_WIDTH - 10);
    }];
    
    [self.arrowImg mas_makeConstraints:^(PywMASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_right).offset(- ARROW_IMG_WIDTH - 10);
        make.right.equalTo(weakSelf.mas_right).offset(-10);
    }];
    
    [self.selectBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        CGFloat width = [self changeDeviceButtonWidth:90];
        make.width.equalTo(@(width));
        make.height.equalTo(weakSelf.mas_height).multipliedBy(0.7);
    }];
    [self.line mas_makeConstraints:^(PywMASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.left.bottom.right.equalTo(weakSelf);
        make.height.equalTo(@0.5);
    }];
}
/**
 *  根据设备不同来确定按钮宽度
 */
- (CGFloat)changeDeviceButtonWidth:(CGFloat)btnWidth{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    if (width < height) {
        btnWidth = (width / 320) * btnWidth;
    }else{
        btnWidth = (width / 568) * btnWidth;
    }
    return btnWidth;
}

#pragma mark - lazyLoad
- (UILabel *)leftLabel{
    if (!_leftLabel) {
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:14];
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.backgroundColor = [UIColor clearColor];
        _leftLabel.textColor = RGB(51, 51, 51, 1);
        _leftLabel.textAlignment = NSTextAlignmentLeft;
        _leftLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    return _leftLabel;
}
-(UILabel *)rightLabel{
    if (!_rightLabel) {
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:14];
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.backgroundColor = [UIColor clearColor];
        _rightLabel.textColor = RGB(153, 153, 153, 1);
        _rightLabel.textAlignment = NSTextAlignmentRight;
        _rightLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    return _rightLabel;
}
- (UIImageView *)arrowImg{
    if (!_arrowImg) {
        _arrowImg = [[UIImageView alloc] init];
        _arrowImg.backgroundColor = [UIColor clearColor];
    }
    return _arrowImg;
}
- (UIButton *)selectBtn{
    if (!_selectBtn) {
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:14];
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.backgroundColor = UIColorFromRGB(0xF57300);
        _selectBtn.layer.cornerRadius = 4.0;
        _selectBtn.layer.masksToBounds = YES;
        _selectBtn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        [_selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [_selectBtn setAccessibilityIdentifier:@"common_switchbtn"];
    }
    return _selectBtn;
}
- (UIView *)line{
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = UIColorFromRGB(0xD2D2D2);
    }
    return _line;
}

@end
